package pers.ascend.spring.cloud.dalston.service.governance.eureka.consumer.feign.manage.controller;

import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pers.ascend.spring.cloud.dalston.service.governance.eureka.consumer.feign.manage.entity.vo.UploadFile2;
import pers.ascend.spring.cloud.dalston.service.governance.eureka.consumer.feign.manage.feign.EurekaProducerFeign;

import javax.validation.constraints.NotNull;

/**
 * 说明：
 *
 * @author ascend
 * 2021/3/9 10:25
 */
@RestController
@RequestMapping(value = "/consumer")
@Validated
public class EurekaConsumerFeignController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private EurekaProducerFeign feign;

    @GetMapping(value = "/hello")
    public String hello(@RequestParam("name") @NotBlank String name) {
        if (log.isInfoEnabled()) {
            log.info("EurekaConsumerFeignController.hello()");
        }
        return feign.hello(name);
    }

    /**
     * 文件上传1
     * <p>
     * 坑1：https://blog.csdn.net/wqj975005563/article/details/103572705
     *
     * @param file File
     * @return String
     */
    @PostMapping(path = "/upload-file1", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String uploadFile1(@RequestPart("file") @NotNull MultipartFile file) {
        if (log.isInfoEnabled()) {
            log.info("EurekaConsumerFeignController.uploadFile1()");
            log.info("文件名称：" + file.getOriginalFilename());
            log.info("文件类型：" + file.getContentType());
            log.info("文件大小：" + file.getSize());
        }
        return feign.uploadFile1(file);
    }


    /**
     * 文件上传2
     *
     * @param uf2 UploadFile2
     * @return String
     */
    @PostMapping(path = "/upload-file2", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String uploadFile2(UploadFile2 uf2) {
        if (log.isInfoEnabled()) {
            log.info("EurekaProducerController.uploadFile2()");
            log.info("文件名称：" + uf2.getFile().getOriginalFilename());
            log.info("文件类型：" + uf2.getFile().getContentType());
            log.info("文件大小：" + uf2.getFile().getSize());
            log.info("上传内容：" + uf2);
        }
        return feign.uploadFile2(uf2.getNickname(), uf2.getFile());
    }
}
